<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\DoctorWithdrawApplyService;
use App\Services\DoctorCashRecordService;

class DoctorWithdrawApplyController extends CommonController{
	private $doctorWithdrawApplyService;
    public function __construct(Request $request){
    	$this->doctorWithdrawApplyService = new DoctorWithdrawApplyService();
    	$this->doctorCashRecordService = new DoctorCashRecordService();
        parent::__construct($request); 
    }
    
    
    /**
     * 管理员列表
     */
    function index(){
    	$name = $this->request->input('name');
    	$status   = $this->request->input('status');
    	$start_time = $this->request->input('start_time');
    	$end_time   = $this->request->input('end_time');
    	
    	$where = array();
    	if(isset($name) && $name !=''){
    		$where[] = array('name','like','%'.$name.'%');
    	}
    	
    	if(isset($status) && $status !==''){
    		$where[] = array('status','=',$status);
    	}
    	
    	if(isset($start_time) && $start_time !=''){
    		$where[] = array('created_at','>=',$start_time);
    	}
    	
    	if(isset($end_time) && $end_time !=''){
    		$where[] = array('created_at','<=',$end_time);
    	}
    	
		$result = $this->doctorWithdrawApplyService->paginateInfo($where,$this->pageSize);
    	return view('doctor.withdraw.index',['data'=>$result]);
    }
    
    /**
     * 添加
     */
    function add(){
    	
    	if($this->request->isMethod('post')){
    		$param = $this->request->except('_token');
    		$result = $this->doctorWithdrawApplyService->addInfo($param);
    		
    		return response()->json($result);
    	}else{
    		return view('doctor.withdraw.add');
    	}
    	
    }
    
    
    /**
     * 编辑
     */
    function edit(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	if($this->request->isMethod('post')){
    		$param = $this->request->except('id','_token');
    		$result = $this->doctorWithdrawApplyService->updateInfo($where,$param);

    		return response()->json($result);
    	}else{
    		$result = $this->doctorWithdrawApplyService->findInfo($where);
    		return view('doctor.withdraw.edit',['info'=>$result['data']]);
    	}
    }
    
    /*
     * 删除
     */
    function delete(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	$result = $this->doctorWithdrawApplyService->deleteInfo($where);
    	return response()->json($result);
    }
    
     /**
     *状态
     */
    function status(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	$param['status'] = $this->request->input('status');
    	$result = $this->doctorWithdrawApplyService->updateInfo($where,$param);
    	if($result['code'] == 1 && $param['status'] == 3){//确认打款 添加打款记录
    		$applyResult = $this->doctorWithdrawApplyService->findInfo(array('id'=>$id));
    	
    		$row = array(
    			'doctor_id'=>$applyResult['data']['doctor_id'],
    			'name'=>$applyResult['data']['name'],
    			'money'=>$applyResult['data']['money'],
    			'type'=>1,
    		);
    		$this->doctorCashRecordService->addInfo($row);
    	}
    	return response()->json($result);
    }
    
    /**
     *上传图片 
     */
    function upload(Request $request){
    	
        $file = $request->file('image');
        $uploadDir = "cate" . '/' .date('Y-m-d');
        $path = $file->store($uploadDir, 'public');
		$path = '/storage/'.$path;
    	return response()->json(['code'=>1,'msg'=>'上传成功','data'=>$path]);
    }
   
}
